﻿Imports System.Data
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Data.OleDb
Imports System
Imports System.Globalization

Partial Class Interfaz_frmBodegaEntradaMateriales
    Inherits System.Web.UI.Page
    Dim CL As New ControladorLogica
    Dim dtDatos As New DataTable
    Dim dataset As New DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If CL.visibilidadesFormsInternos("Bodega", Session("user").ToString) = False Then
            Response.Redirect("Index.aspx")
        End If
        Dim ArrayMaterial As New ArrayList
        Dim ArrayFamilia As New ArrayList
        Dim arrayOC As New ArrayList

        ArrayFamilia = CL.ObtenerFamilia()
        ArrayMaterial = CL.ObtenerMaterial()
        arrayOC = CL.ObtenerOrdenCompra()

        If Session("EntradaMateriales") Is Nothing Then
            Session("EntradaMateriales") = dtDatos
        Else
            dtDatos = DirectCast(Session("EntradaMateriales"), DataTable)

        End If


        If Not Page.IsPostBack Then
            Me.DropEscojer.Items.Add("Manual")
            Me.DropEscojer.Items.Add("Automatico")

            For i As Integer = 0 To ArrayFamilia.Count - 1
                Me.DropFamilias.Items.Add(DirectCast(ArrayFamilia.Item(i), Familia).Nombre)
            Next

            For i As Integer = 0 To ArrayMaterial.Count - 1
                Me.DropCodigoMateriales.Items.Add(DirectCast(ArrayMaterial.Item(i), Material).Codigo)
            Next

            For i As Integer = 0 To arrayOC.Count - 1
                Me.DropOC.Items.Add(DirectCast(arrayOC.Item(i), OrdenCompra).NumeroDeOrden)
            Next
            CargarDropFilaColumna()

            dtDatos.Columns.Add(New DataColumn("ITEM"))
            dtDatos.Columns.Add(New DataColumn("UNIDAD"))
            dtDatos.Columns.Add(New DataColumn("DETALLE"))
            dtDatos.Columns.Add(New DataColumn("CANTIDAD"))
            dtDatos.Columns.Add(New DataColumn("PRECIOUNITARIO"))
            dtDatos.Columns.Add(New DataColumn("CODMATERIAL"))
            dtDatos.Columns.Add(New DataColumn("FILA"))
            dtDatos.Columns.Add(New DataColumn("COLUMNA"))

            Me.GridBodega.DataBind()
            Me.GridBodega.DataSource = Me.dtDatos
            Me.PanelAutomatico.Visible = False
            Me.PanelManual.Visible = False
        End If


    End Sub


#Region "Funcionalidades"
    Public Sub Mensaje(ByVal msg As String)
        Dim alertScript As String = "<script language=JavaScript runat=server>"
        alertScript &= "alert('" & msg & "');"
        alertScript &= "</script>"
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "clientScrip", alertScript)
    End Sub

    Private Sub CargarDropFilaColumna()
        Me.DropFila.Items.Add("A")
        Me.DropFila.Items.Add("B")
        Me.DropFila.Items.Add("C")
        Me.DropFila.Items.Add("D")
        Me.DropFila.Items.Add("E")
        Me.DropFila.Items.Add("F")
        Me.DropFila.Items.Add("G")
        Me.DropFila.Items.Add("H")
        Me.DropFila.Items.Add("I")
        Me.DropFila.Items.Add("J")
        Me.DropFila.Items.Add("K")
        Me.DropFila.Items.Add("L")
        Me.DropFila.Items.Add("M")
        Me.DropFila.Items.Add("N")
        Me.DropFila.Items.Add("Ñ")
        Me.DropFila.Items.Add("O")
        Me.DropFila.Items.Add("P")
        Me.DropFila.Items.Add("Q")
        Me.DropFila.Items.Add("R")
        Me.DropFila.Items.Add("S")
        Me.DropFila.Items.Add("T")
        Me.DropFila.Items.Add("U")
        Me.DropFila.Items.Add("V")
        Me.DropFila.Items.Add("W")
        Me.DropFila.Items.Add("X")
        Me.DropFila.Items.Add("Y")
        Me.DropFila.Items.Add("Z")

        For i As Integer = 1 To 20
            Me.DropColumna.Items.Add(i)
        Next
    End Sub

    Private Sub Limpiar()
        Me.TxtCantidad.Text = ""
        Me.TxtDetalle.Text = ""
    End Sub

    Private Sub Escojer()
        Dim opcion As String = Me.DropEscojer.Text

        If opcion = "Manual" Then
            Me.PanelManual.Visible = True
            Me.PanelAutomatico.Visible = False
        Else
            If opcion = "Automatico" Then
                Me.PanelManual.Visible = False
                Me.PanelAutomatico.Visible = True
            End If

        End If
    End Sub

    Private Sub InsertarBD()
        Dim filaGrid As GridViewRow
        Dim Nombre As String
        Dim Descripcion As String = Me.TxtDetalle.Text
        Dim Cantidad As String = Me.TxtCantidad.Text
        Dim FechaIngreso As String = Me.DateFecha.Text
        Dim UbicacionColumna As String = Me.DropFila.Text
        Dim UbicacionFila As String = Me.DropColumna.Text
        Dim indicador As String
        Dim query As String

        Dim MaterialDatos As New MaterialDatos()

        If Me.DropEscojer.Text = "Manual" = 0 Then
            MaterialDatos.Cantidad = Cantidad
            MaterialDatos.FechaIngreso = FechaIngreso
            MaterialDatos.UbicacionColumna = UbicacionColumna
            MaterialDatos.UbicacionFila = UbicacionFila
            MaterialDatos.FkBodega = "1"
            MaterialDatos.FkOrdenCompra = ""
            MaterialDatos.Descripcion = Me.TxtDetalle.Text

            If CheckCodigo.Checked = True Then
                query = "select id from material where codmaterial='" + Me.DropCodigoMateriales.Text + "'"
                MaterialDatos.FkMaterial = CL.Ejecuta(query).Item("id").ToString
                CL.InsertarMaterialDatos(MaterialDatos)
            Else
                If Me.CheckFamilia.Checked = True Then
                    query = "select id from material where nombre='" + Me.DropMaterial.Text + "'"
                    MaterialDatos.FkMaterial = CL.Ejecuta(query).Item("id").ToString
                    CL.InsertarMaterialDatos(MaterialDatos)
                End If
            End If
        Else
            With Me.GridBodega
                ' Recorrer las filas del dataGridView  
                For fila As Integer = 0 To .Rows.Count - 1
                    filaGrid = Me.GridBodega.Rows(fila)
                    Dim cb As CheckBox = filaGrid.FindControl("itemSelector")

                    'Recorrer la cantidad de columnas que contiene el dataGridView 
                    If cb.Checked = True Then

                        Dim item As String = filaGrid.Cells(1).Text
                        Dim unidad As String = filaGrid.Cells(2).Text
                        Dim detalle As String = filaGrid.Cells(3).Text
                        Dim cant As String = filaGrid.Cells(4).Text
                        Dim precioUnitario As String = filaGrid.Cells(5).Text
                        Dim codMat As String = filaGrid.Cells(6).Text
                        Dim fil As String = filaGrid.Cells(1).Text
                        Dim columna As String = filaGrid.Cells(8).Text
                        Dim detal As String = filaGrid.Cells(9).Text

                        MaterialDatos.Cantidad = cant
                        MaterialDatos.FechaIngreso = FechaIngreso
                        MaterialDatos.UbicacionColumna = fil
                        MaterialDatos.UbicacionFila = columna
                        MaterialDatos.FkBodega = "1"
                        MaterialDatos.FkOrdenCompra = DropOC.Text
                        MaterialDatos.Descripcion = detal

                        query = "select id from material where codmaterial='" + codMat + "'"
                        MaterialDatos.FkMaterial = CL.Ejecuta(query).Item("id").ToString
                        CL.InsertarMaterialDatos(MaterialDatos)
                    End If
                Next fila
            End With

        End If

        Me.Mensaje("El ingreso del material se ha realizado correctamente")

        indicador = ""
        Limpiar()
    End Sub

    Private Sub CargarOrdenCompra()
        Dim numeroOC As String = Me.DropOC.Text
        Dim query As String = "select * from ordencompra where numorden =" + numeroOC
        Dim arrayOC As ArrayList = CL.ObtenerOrdenCompra(query)

        query = "select * from itemordencompra where fkordencompra=" + numeroOC
        Dim arrayItemOC As ArrayList = CL.obtenerItemOrdenCompra(query)
        Dim i As Integer

        Me.TxTFechaPedido.Text = DirectCast(arrayOC.Item(0), OrdenCompra).FechaDePedido

        vaciaGrid()

        For i = 0 To arrayItemOC.Count - 1
            Dim drDatos As DataRow = dtDatos.NewRow
            Dim item As ItemDocumentos = DirectCast(arrayItemOC.Item(i), ItemDocumentos)
            query = "select codmaterial from material where codmaterial ='" + item.Fkmaterial + "'"
            Dim codMaterial As String = CL.Ejecuta(query).Item("codmaterial").ToString

            drDatos.Item("ITEM") = item.ITEM
            drDatos.Item("UNIDAD") = item.Unidad
            drDatos.Item("DETALLE") = item.Detalle
            drDatos.Item("CANTIDAD") = item.Cantidad
            drDatos.Item("PRECIOUNITARIO") = item.PrecioUnitario
            drDatos.Item("CODMATERIAL") = codMaterial
            drDatos.Item("FILA") = "Inserte Fila"
            drDatos.Item("COLUMNA") = "Inserte Columna"

            dtDatos.Rows.Add(drDatos)

            Me.GridBodega.DataSource = dtDatos
            Me.GridBodega.DataBind()
        Next


    End Sub

    Private Sub BuscarItemFamilia()
        Dim query As String = "select id from familiamateriales where nombre='" + Me.DropFamilias.Text + "'"
        Dim refFamilia As String = CL.Ejecuta(query).Item("id").ToString
        query = "select * from material where reffamilia = " + refFamilia
        Dim arrayMaterial As ArrayList = CL.ObtenerMaterial(query)

        Me.DropMaterial.Items.Clear()
        If arrayMaterial.Count <> 0 Then
            For i As Integer = 0 To arrayMaterial.Count - 1
                Dim item As Material = DirectCast(arrayMaterial.Item(0), Material)
                Me.DropMaterial.Items.Add(item.Nombre)
            Next
        Else
            Me.Mensaje("No hay materiales asociados a la familia seleccionada")
        End If

    End Sub

    Private Sub vaciaGrid()
        dtDatos = DirectCast(Session("EntradaMateriales"), DataTable)
        If Not dtDatos Is Nothing Then
            dtDatos.Clear()
            Session("EntradaMateriales") = dtDatos
        End If
        Me.GridBodega.DataSource = Nothing
        Me.GridBodega.DataBind()
    End Sub

#End Region

#Region "Botones"

    Protected Sub BtnBuscar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnBuscar.Click
        BuscarItemFamilia()
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        InsertarBD()
    End Sub

    Protected Sub BttnEscojer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BttnEscojer.Click
        Escojer()
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Limpiar()
    End Sub

    Protected Sub BtnOC_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnOC.Click
        CargarOrdenCompra()
    End Sub
#End Region

#Region "Eventos Grid"
    'Grid Material
    Protected Sub GridBodega_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        GridBodega.PageIndex = e.NewPageIndex
        'Bind data to the GridView control.
        GridBodega.DataSource = Session("EntradaMateriales")
        GridBodega.DataBind()
    End Sub

    Protected Sub GridBodega_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        'Set the edit index.
        GridBodega.EditIndex = e.NewEditIndex
        'Bind data to the GridView control.
        GridBodega.DataSource = Session("EntradaMateriales")
        GridBodega.DataBind()
    End Sub

    Protected Sub GridBodega_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
        'Reset the edit index.
        GridBodega.EditIndex = -1
        'Bind data to the GridView control.
        GridBodega.DataSource = Session("EntradaMateriales")
        GridBodega.DataBind()
    End Sub

    Protected Sub GridBodega_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
        'Retrieve the table from the session object.
        Dim dt As DataTable = CType(Session("EntradaMateriales"), DataTable)

        'Update the values.
        Dim row As GridViewRow = GridBodega.Rows(e.RowIndex)

        Dim ITEM As String = (CType((row.Cells(1).Controls(0)), TextBox)).Text.ToString
        Dim UNIDAD As String = (CType((row.Cells(2).Controls(0)), TextBox)).Text
        Dim DETALLE As String = (CType((row.Cells(3).Controls(0)), TextBox)).Text
        Dim CANTIDAD As String = (CType((row.Cells(4).Controls(0)), TextBox)).Text
        Dim PRECIOUNITARIO As String = (CType((row.Cells(5).Controls(0)), TextBox)).Text
        Dim CODMATERIAL As String = (CType((row.Cells(6).Controls(0)), TextBox)).Text
        Dim FILA As String = (CType((row.Cells(8).Controls(0)), TextBox)).Text
        Dim COLUMNA As String = (CType((row.Cells(9).Controls(0)), TextBox)).Text


        dt.Rows(row.DataItemIndex)("ITEM") = ITEM
        dt.Rows(row.DataItemIndex)("UNIDAD") = UNIDAD
        dt.Rows(row.DataItemIndex)("DETALLE") = DETALLE
        dt.Rows(row.DataItemIndex)("CANTIDAD") = CANTIDAD
        dt.Rows(row.DataItemIndex)("PRECIOUNITARIO") = PRECIOUNITARIO
        dt.Rows(row.DataItemIndex)("CODMATERIAL") = CODMATERIAL
        dt.Rows(row.DataItemIndex)("FILA") = FILA
        dt.Rows(row.DataItemIndex)("COLUMNA") = COLUMNA

        'Reset the edit index.
        GridBodega.EditIndex = -1

        'Bind data to the GridView control.
        GridBodega.DataSource = Session("EntradaMateriales")
        GridBodega.DataBind()
    End Sub

    Sub GridBodega_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
        Dim dt As DataTable = CType(Session("EntradaMateriales"), DataTable)
        dt.Rows(e.RowIndex).Delete()
        GridBodega.DataSource = dt
        GridBodega.DataBind()
    End Sub
#End Region

    
End Class
